<script>
	import {
		get_config_find
	} from '@/request/api.js'
	export default {
		onLaunch: function(){
			
			var _this = this;
			//查看是否需要更新
			// #ifdef APP-PLUS
			get_config_find().then(data => {
					console.log(data);
				if(data.code == 200){  
						plus.runtime.getProperty(plus.runtime.appid, function(inf) {
							var onlineVersion = data.find.site_banben;
							var localVersion = inf.version;
							// console.log(localVersion,22222);
							// localVersion = localVersion.version
							var isUpload = false; 
							for (let i = 0;i < onlineVersion.length; i++) { 
								if (parseInt(onlineVersion[i]) > parseInt(localVersion[i])) {
									isUpload = true
								}
							}
							console.log(onlineVersion,222222222222);
							if (isUpload) {
								uni.showModal({
									title: "发现新版本",
									content: "确认下载更新", 
									showCancel: false, 
									success: (res) => {   
										if (res.confirm == true) { //当用户确定更新，执行更新
											_this.doUpData(data.find.site_xiazai);
										}
									}
								})
							}
						});
					
				
				}
			});
	
		// #endif
			
		},
		methods: {
			doUpData(e){
				uni.showLoading({
					title: '更新中……'
				})
				//return;
				uni.downloadFile({ //执行下载
					url: e, //下载地址
					success: downloadResult => { //下载成功 
						uni.hideLoading();
						if (downloadResult.statusCode == 200) {
							uni.showModal({
								title: '',
								content: '更新成功，确定现在重启吗？',
								confirmText: '重启',
								confirmColor: '#EE8F57',
								success: function(res) {
									if (res.confirm == true) {
										plus.runtime.install( //安装  
											downloadResult.tempFilePath, {
												force: true
											},
											function(res) {
												utils.showToast('更新成功，重启中');
												plus.runtime.restart();
											}
										);
									}
								}
							});
						}
					}
				});
			}
		}
	}
</script>

<style>
	@import '@/common/css/reset.css';
	@import '@/common/css/common.css';
	uni-toast .uni-simple-toast__text{
	word-break: break-word;
	}
</style> 
